<template>
  <div class="page testPage">
    <div class="scroll-view">
      <customHeader middleTitle="测试app接口页面" />
      <div class="page-body">
        <mt-button type="default"
                   @click="handleScan">扫一扫获取二维码信息</mt-button>
        <input v-if="isAndroid"
               type="text"
               v-model="serverUrl"
               placeholder="设置服务地址">
        <mt-button v-if="isAndroid"
                   type="default"
                   @click="handleSetServerUrl">设置服务地址</mt-button>
        <mt-button v-if="isAndroid"
                   type="default"
                   @click="handleRemoveH5Source">删除本地H5</mt-button>
        <mt-button v-if="isAndroid"
                   type="default"
                   @click="handleReset">重置所有</mt-button>
        <input type="text"
               v-if="isAndroid"
               v-model="h5ZipUrl"
               placeholder="http://H5资源服务器地址+:端口+/">
        <input type="text"
               v-if="!isAndroid"
               v-model="h5ZipUrl"
               placeholder="http://H5资源服务器地址+:端口+/">
        <mt-button type="default"
                   v-if="isAndroid"
                   @click="handleDownLoadH5Source">下载H5资源包</mt-button>
        <input type="text"
               v-model="localUrl"
               placeholder="pushWindow打开页面">
        <mt-button type="default"
                   @click="handleOpenLocalUrl">local url</mt-button>
        <mt-button type="default"
                   @click="handleOpenLocalUrlTitle">loacl url带title</mt-button>
        <mt-button type="default"
                   @click="handleOpenServerUrl">server url</mt-button>
        <mt-button type="default"
                   @click="handleOpenServerUrlTitle">server url带title</mt-button>
        <mt-button type="default"
                   @click="handleCopySessionId">复制sessionId</mt-button>
        <mt-button type="default"
                   @click="handleCopyUserAgent">复制userAgent</mt-button>
        <mt-button type="primary" icon="home"
                   @click="()=>{
                     this.$router.push('/');
                   }">跳转调试首页，新模块调试用</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import customHeader from "../../components/header/header";
import { Actionsheet, Button, Toast } from "mint-ui";
Vue.component(Actionsheet.name, Actionsheet);
import jsBridge from "../../tools/jsBridge.js";
import config from "../../base/profile/config.json";
import VueClipboard from "vue-clipboard2";
Vue.use(VueClipboard);
export default {
  name: "testPages",
  data() {
    return {
      localUrl: "",
      suffixParams: "",
      h5ZipUrl:config.h5Source,
      serverUrl: window.APP.serverUrl,
      isAndroid: false,
      sessionId:window.APP.sessionId,
    };
  },
  methods: {
    handleScan() {
      jsBridge.scanQRCode({ type: "bar" }, response => {
        if (
          response &&
          (response.success === true || response.success === "true")
        ) {
          const result = response.resultObject;
          this.$copyText(result).then(
            e => {
              Toast("成功复制***********" + result);
            },
            e => {
              Toast("您的系统版本过低");
            }
          );
        } else {
          Toast("扫码失败");
        }
      });
    },
    handleStopBroadcast() {
      AppUtil.call(AppUtil.APIS.STOP_BROADCAST, {}, function(result) {

      });
    },
    handleStartBroadcast() {
      AppUtil.call(
        AppUtil.APIS.START_BROADCAST,
        {
          type: "setUpNetwork",
          content: '{"name":"YunZong","password":"YUNZONGNET@WIFI"}'
        },
        function(result) {

        }
      );
    },
    handleGetNearbyWIFI() {
      AppUtil.call(AppUtil.APIS.GET_NEARBY_WIFI, {}, function(result) {

      });
    },
    getPlatform() {
      let ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf("android") >= 0) {
        this.isAndroid = true;
      } else {
        this.isAndroid = false;
      }
    },
    handleSetServerUrl() {
      AppUtil.call(
        AppUtil.APIS.SET_SERVER_URL,
        { url: this.serverUrl },
        function(result) {

        }
      );
    },
    handleRemoveH5Source() {
      AppUtil.call(AppUtil.APIS.REMOVE_H5_SOURCE, {}, function(result) {

      });
    },
    handleReset() {
      AppUtil.call(AppUtil.APIS.RESET_ALL, {}, function(result) {

      });
    },
    handleDownLoadH5Source() {
      let t = new Date().getTime();
      let url = this.isAndroid?this.h5ZipUrl+"unzip.zip":this.h5ZipUrl+"H5.zip";
      AppUtil.call(
        AppUtil.APIS.DOWNLOAD_H5_SOURCE,
        { url: url+'?t='+t},
        function(result) {

        }
      );
    },
    handleOpenLocalUrl() {
      jsBridge.openWindow({
        url: this.localUrl,
        target: "local",
        suffixParams: this.suffixParams
      });
    },
    handleOpenLocalUrlTitle() {
      jsBridge.openWindow({
        url: this.localUrl,
        target: "local",
        needTitle: true,
        suffixParams: this.suffixParams
      });
    },
    handleOpenServerUrl() {
      jsBridge.openWindow({
        url: this.localUrl,
        target: "server"
      });
    },
    handleOpenServerUrlTitle() {
      jsBridge.openWindow({
        url: this.localUrl,
        target: "server",
        needTitle: true,
        suffixParams: this.suffixParams
      });
    },
    handleCopySessionId() {
      const result=window.APP.sessionId;
      this.$copyText(result).then(
            e => {
              Toast("复制成功");
            },
            e => {
              Toast("您的系统不支持");
            }
      );
    },
    handleCopyUserAgent() {
      const result=window.navigator.userAgent;
      this.$copyText(result).then(
            e => {
              Toast("复制成功");
            },
            e => {
              Toast("您的系统不支持");
            }
      );
    }
  },
  mounted() {
    this.getPlatform();
  },
  components: {
    customHeader
  }
};
</script>
<style lang='scss' scoped>
@import "testPage";
</style>
